﻿using System;
using System.Collections.Generic;
using System.Linq;


namespace H.Cmd
{

    /// <summary>
    /// 封装一个命令行参数,用于方便进行命令行参数解析
    /// </summary>
    [Serializable]
    public class Arguments : ICloneable
    {
        /// <summary>
        /// 存储准备被解析的命令行参数数组
        /// </summary>
        private readonly string[] _Args;

        /// <summary>
        /// 获取指定下标的字符串
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string this[int index] => _Args[index];

        /// <summary>
        /// 获取参数的个数
        /// </summary>
        public int Count => _Args.Length;

        /// <summary>
        /// 初始化一个新的参数解析器
        /// </summary>
        /// <param name="args"></param>
        public Arguments(string[] args)
        {
            _Args = args;
        }

        /// <summary>
        /// 查找某个可选项的下标,以便进行读取
        /// </summary>
        /// <param name="startIndex">传入开始查找的下标</param>
        /// <param name="ignoreCase"></param>
        /// <returns>如果查找到返回大于等于0的值,表示找到的下标,如果没有找到返回-1</returns>
        public int IndexOfOption(string optionName, int startIndex = 0, bool ignoreCase = false)
        {
            if (ignoreCase)
            {
                return Array.FindIndex(_Args, startIndex, s => string.Compare(s, optionName, true) == 0);
            }
            else
            {
                return Array.IndexOf(_Args, optionName, startIndex);
            }
        }

        /// <summary>
        /// 获取从指定下标开始, <paramref name="count">count</paramref>个参数
        /// </summary>
        /// <param name="startIndex">开始位置</param>
        /// <param name="count">获取的个数,如果为0,则表示一直获取到结尾</param>
        /// <returns>返回获取到的参数字符串副本</returns>
        public string[] GetOptions(int startIndex, int count = 0)
        {
            if (count > 0)
            {
                return _Args.Skip(startIndex).Take(count).ToArray();
            }
            else
            {
                return _Args.Skip(startIndex).ToArray();
            }
        }


        /// <summary>
        /// 获取所有可选项的参数
        /// </summary>
        /// <param name="optionName">可选项的名称</param>
        /// <param name="startIndex">开始查找的下标</param>
        /// <param name="appendCount">每个可选项的参数长度</param>
        /// <param name="maxIndex">可选项的参数跨度(用于确定必填项的位置)</param>
        /// <param name="ignoreCase">是否以不区分大小写进行查找</param>
        /// <returns>返回找到的所有参数</returns>
        public string[][] GetNamedOptions(string optionName, int startIndex, int appendCount, ref int maxIndex, bool ignoreCase = false)
        {
            int _maxIndex = startIndex;
            int updateMaxIndex(int index)
            {
                if (index != -1)
                {
                    _maxIndex = Math.Max(index + appendCount + 1, _maxIndex);
                }
                return index;
            }
            int cursor = startIndex;
            List<string[]> ret = new List<string[]>();
            while (cursor < _Args.Length)
            {
                cursor = updateMaxIndex(IndexOfOption(optionName, cursor, ignoreCase));
                if (cursor == -1)
                {
                    break;
                }
                else
                {
                    ret.Add(GetOptions(++cursor, appendCount));
                }
            }

            maxIndex = Math.Max(maxIndex, _maxIndex);
            return ret.ToArray();
        }

        public object Clone()
        {
            throw new NotImplementedException();
        }
    }
}
